package org.game.netty.server;

import org.game.module.core.Core;
import org.game.protobuf.s2c.S2CLoginMsg.Pong;

import com.sojoys.artifact.factory.message.IMessage;
import com.sojoys.artifact.manager.MessageManager;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author : DengYing
 * @CreateDate : 2017年12月25日 下午2:12:47
 * @Description ：心跳处理器
 */
public class HeartbeatHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		if (msg instanceof IMessage) {
			IMessage imsg = (IMessage) msg; 
			if (imsg.getCommandId() == Core.heartbeat) {
				Pong.Builder builder = Pong.newBuilder();
				IMessage out = MessageManager.me().create(imsg.getCommandId(), builder.build().toByteArray());
				ctx.channel().writeAndFlush(out);
			}else {
				ctx.fireChannelRead(msg);
			}
		}
	}

}
